package leetcode100;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

// TODO 递归+回溯 | 深度优先遍历 【-】 电话号码的字母组合
// TODO https://leetcode.cn/problems/letter-combinations-of-a-phone-number/solution/leetcode-17-letter-combinations-of-a-phone-number-/

public class lc100_0017 {

    String[] phone = new String[]{
            "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"
    };
    private ArrayList<String> res = new ArrayList<String>();
    StringBuilder path = new StringBuilder("");

    public List<String> letterCombinations(String digits) {
        int n = digits.length();
        if(n == 0)
            return res;

        dfs(digits, 0, n);
        return res;
    }

    // 递归
    private void dfs(String digits, int i, int n){
        // 递归结束控制器
        if(i == n){
            res.add(new String(path));
            return;
        }

        String phoneStr = phone[digits.charAt(i) - '0'];
        for(int j = 0 ; j < phoneStr.length() ; j++){
            path.append(phoneStr.charAt(j));
            dfs(digits, i+1, n);
            path.deleteCharAt(path.length() - 1);
        }
    }
}